package com.ruoyi.web.controller.airplane;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TbDevice;
import com.ruoyi.system.domain.vo.RelDeviceVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TbAirplane;
import com.ruoyi.system.service.ITbAirplaneService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 飞机信息Controller
 *
 * @author ruoyi
 * @date 2025-07-23
 */
@Api(tags = "飞机信息管理")
@RestController
@RequestMapping("/system/airplane")
public class TbAirplaneController extends BaseController {
    @Autowired
    private ITbAirplaneService tbAirplaneService;

    /**
     * 查询飞机信息列表
     */
    //@PreAuthorize("@ss.hasPermi('system:airplane:list')")
//    @ApiOperation("获取飞机列表")
//    @GetMapping("/list")
//    public TableDataInfo list(TbAirplane tbAirplane) {
//        startPage();
//        List<TbAirplane> list = tbAirplaneService.selectTbAirplaneList(tbAirplane);
//        return getDataTable(list);
//    }
    @ApiOperation("获取飞机列表分页")
    @GetMapping("/list")
    public TableDataInfo listPage(TbAirplane tbAirplane) {
        startPage();
        List<TbAirplane> list = tbAirplaneService.selectTbAirplaneLis(tbAirplane);
        return getDataTable(list);
    }


    /**
     * 导出飞机信息列表
     */
    //  @PreAuthorize("@ss.hasPermi('system:airplane:export')")
    @ApiOperation("导出飞机信息列表")
    @Log(title = "飞机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbAirplane tbAirplane) {
        List<TbAirplane> list = tbAirplaneService.selectTbAirplaneList(tbAirplane);
        ExcelUtil<TbAirplane> util = new ExcelUtil<TbAirplane>(TbAirplane.class);
        util.exportExcel(response, list, "飞机信息数据");
    }

    /**
     * 获取飞机信息详细信息
     */
    //@PreAuthorize("@ss.hasPermi('system:airplane:query')")
    @ApiOperation("获取飞机信息详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tbAirplaneService.selectTbAirplaneById(id));
    }


    @ApiOperation("查询飞机可关联设备列表")
    @GetMapping("/getDeviceList")
    public R<List<TbDevice>> getDeviceList() {
        return tbAirplaneService.selectCanRelDev();
    }

    @ApiOperation("设备关联飞机")
    @PostMapping("relDevice")
    public AjaxResult relDevice(@RequestBody RelDeviceVO vo) {
        return toAjax(tbAirplaneService.relDevice(vo.getPlaneCode(), vo.getDeviceId()));
    }


    @ApiOperation("修改设备状态")
    @PostMapping("updateStatus")
    public AjaxResult updateStatus(Long id, Integer status) {
        return toAjax(tbAirplaneService.updateStatusById(id, status));
    }


    /**
     * 新增飞机信息
     */
    //@PreAuthorize("@ss.hasPermi('system:airplane:add')")
    @ApiOperation("新增飞机信息")
    @Log(title = "飞机信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Valid TbAirplane tbAirplane) {
        TbAirplane plane = tbAirplaneService.selectTbAirplaneCode(tbAirplane.getPlaneCode());
        if (plane != null) {
            return error("飞机号已存在,不能重复添加!");
        }
        tbAirplane.setStatus(1);
        tbAirplane.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        tbAirplane.setCreateTime(DateUtils.getNowDate());
        tbAirplane.setRegistrationTime(DateUtils.getNowDate());
        return toAjax(tbAirplaneService.insertTbAirplane(tbAirplane));
    }

    /**
     * 修改飞机信息
     */
    // @PreAuthorize("@ss.hasPermi('system:airplane:edit')")
    @Log(title = "飞机信息", businessType = BusinessType.UPDATE)
    @ApiOperation("修改飞机信息")
    @PutMapping
    public AjaxResult edit(@RequestBody TbAirplane tbAirplane) {
        tbAirplane.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        tbAirplane.setUpdateTime(DateUtils.getNowDate());
        //  tbAirplane.setRegistrationTime(DateUtils.getNowDate());
        return toAjax(tbAirplaneService.updateTbAirplane(tbAirplane));
    }

    /**
     * 删除飞机信息
     */
    // @PreAuthorize("@ss.hasPermi('system:airplane:remove')")
    @Log(title = "飞机信息", businessType = BusinessType.DELETE)
    @ApiOperation("删除飞机信息")
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tbAirplaneService.deleteTbAirplaneByIds(ids));
    }


    @ApiOperation("获取飞机号下拉框")
    @GetMapping("/get/airplaneCode")
    public List<String> listAirPlaneCode() {

        List<String> list = tbAirplaneService.listAirpalneCode();
        return list;
    }
}
